package com.sky.service.impl;

import com.sky.constant.MessageConstant;
import com.sky.context.BaseContext;
import com.sky.entity.AddressBook;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.AddressBookMapper;
import com.sky.service.AddressBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.security.auth.DestroyFailedException;
import java.util.List;

/**
 * 地址簿管理业务层
 */
@Service
public class AddressBookServiceImpl implements AddressBookService {

    @Autowired
    private AddressBookMapper addressBookMapper ;

    /**
     * 根据用户id查询对应的地址
     *
     * @return
     */
    @Override
    public List<AddressBook> list() {
        //获取当前登录用户的 id
        Long userId = BaseContext.getCurrentId();
        //根据用户id查询地址
        List<AddressBook> addressBookList = addressBookMapper.list(userId) ;
        return addressBookList ;
    }

    /**
     * 新增地址簿地址
     *
     * @param addressBook
     */
    @Override
    public void save(AddressBook addressBook) {
        //补全字段
        addressBook.setUserId(BaseContext.getCurrentId()) ;
        addressBook.setIsDefault(0);
        //将地址信息保存进数据库
        addressBookMapper.save(addressBook) ;
    }

    /**
     * 获取默认地址
     *
     * @return
     */
    @Override
    public AddressBook getDefaultAddress() {
        //获取登录用户的 id
        Long userId = BaseContext.getCurrentId() ;
        //获取默认地址
        AddressBook addressBook = addressBookMapper.getDefaultAddress(userId) ;
        return addressBook ;
    }

    /**
     * 设置默认地址
     *
     * @param addressBook
     */
    @Override
    public void setDefaultAddress(AddressBook addressBook) {
        //查看当前是否已经有其他的地址被设置为默认地址 如果有 就修改为0
        Long userId = BaseContext.getCurrentId();   //获取当前登录用户的id
        List<AddressBook> list = addressBookMapper.list(userId);
        list.forEach(item -> {
            if (item.getIsDefault() == 1) {
                item.setIsDefault(0);
                addressBookMapper.update(item);
            }
        });
        //根据传进来的地址 id 将地址表中对应的行的is_default 字段设置为 1
        addressBook.setIsDefault(1);
        addressBookMapper.update(addressBook) ;  //用 update 后面可以复用这个函数
    }

    /**
     * 根据id删除地址
     *
     * @param id
     */
    @Override
    public void deleteById(Long id) throws DestroyFailedException {
        Long userId = BaseContext.getCurrentId();   //获取当前的登录用户的id
        AddressBook addressBook = addressBookMapper.getById(id);
        if (addressBook.getIsDefault() == 1) {
            throw new DeletionNotAllowedException(MessageConstant.ADDRESS_IS_DEFAULT);
        }
        addressBookMapper.deleteById(id , userId) ;
    }

    /**
     * 根据id查询地址
     *
     * @param id
     * @return
     */
    @Override
    public AddressBook getById(Long id) {
        Long userId = BaseContext.getCurrentId();   //获取登录用户 id
        AddressBook addressBook = addressBookMapper.getById(id) ;
        return addressBook ;
    }

    /**
     * 根据id修改地址信息
     *
     * @param addressBook
     */
    @Override
    public void update(AddressBook addressBook) {
        addressBookMapper.update(addressBook);
    }
}
